﻿using Enterprise_Get.API.Application.Commands.MainPlanToTable;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.XSSF.UserModel;
using THE_WIND_RISES.Common.Domans.BasicInformation.MainPlans;
using THE_WIND_RISES.Common.Dtos;


namespace Enterprise_Get.API.Controllers.Week2
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MainplanController : ControllerBase
    {
        public MainplanController(IMediator mediator, ILogger<MainplanController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        public IMediator mediator { get; set; }
        public ILogger<MainplanController> logger { get; set; }
    
        /// <summary>
        /// 生成主生产计划Excel表格
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> ShowExcelMainplan()
        {
            try
            {
                //获取需要导出的数据
                Result list = await mediator.Send(new ShowMainPlanExcelCommand());
                //转换数据类型
                List<Mainpalan> ExcelData=list.Data as List<Mainpalan>;
                //实例化一个Excel表格
                var WB=new XSSFWorkbook();
                var sheet = WB.CreateSheet("主生产计划表");
                var row=sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("计划编码");
                row.CreateCell(2).SetCellValue("计划名称");
                row.CreateCell(3).SetCellValue("计划描述");
                row.CreateCell(4).SetCellValue("状态");
                row.CreateCell(5).SetCellValue("制单人");
                row.CreateCell(6).SetCellValue("制单部门");
                row.CreateCell(7).SetCellValue("创建时间");
                row.CreateCell(8).SetCellValue("审批状态");
                row.CreateCell(8).SetCellValue("操作人");
                row.CreateCell(9).SetCellValue("操作内容");
                row.CreateCell(10).SetCellValue("操作时间");
                row.CreateCell(11).SetCellValue("审计字段");
            int i = 0;
                foreach (Mainpalan item in ExcelData)
                {
                    i++;
                    var rows = sheet.CreateRow(i);
                    rows.CreateCell(0).SetCellValue(item.MainplanId);
                    rows.CreateCell(1).SetCellValue(item.MainplanNum);
                    rows.CreateCell(2).SetCellValue(item.MainplanName);
                    rows.CreateCell(3).SetCellValue(item.MainplanContext);
                    rows.CreateCell(4).SetCellValue(item.MainplanState==1? "新建" : (item.MainplanState == 2 ? "确认" : "变更"));
                    rows.CreateCell(5).SetCellValue(item.CreateName);
                    rows.CreateCell(6).SetCellValue(item.CreateSection);
                    rows.CreateCell(7).SetCellValue(item.CreateTime.ToString("yyyy年MM月dd日"));
                    rows.CreateCell(8).SetCellValue(item.ApprovalState==true?"禁用":"启用");
                    rows.CreateCell(9).SetCellValue(item.ApprovalState == true ? "禁用" : "启用");
                    rows.CreateCell(10).SetCellValue(item.Handle);
                    rows.CreateCell(11).SetCellValue(item.HandleContext);
                    rows.CreateCell(12).SetCellValue(item.HandleTime.ToString());
                    rows.CreateCell(13).SetCellValue(item.Pid == 1 ? "创建" : (item.Pid == 2 ? "审批" : (item.Pid==3? "删除" : "修改")));
                }
                byte[] bytes;
                using (var ms = new MemoryStream())
                { 
                    WB.Write(ms);
                    bytes = ms.ToArray();
                }
                var main = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(bytes, main, "主生产计划表.xlsx");
            }
            catch (Exception ex)
            {
                logger.LogError("生成Excel表格失败");
                throw;
            }
        }

    }
}
